﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace my_english
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        int sb = 0;
        string Respond_date, judgment;
        // 在Form2中定义委托
        public delegate void DataPassedDelegate(string data);
        string filePath = "./English.txt";//单词记录本  

        //私有变量和赋值，value值在父窗口传递
        private string w_str;
        public string str
        {
            set
            {
                w_str = value;
            }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("数据："+w_str);
            if (string.Equals(this.textBox1.Text, judgment, StringComparison.OrdinalIgnoreCase))//判断单词是否正确
            {
                MessageBox.Show("正确！！");
                this.Close();
                //this.Hide();
                //this.label1.Text = "";
                //this.textBox1.Text = "";
                //this.label3.Text = "";
            }
            else
            {
                MessageBox.Show("再想想!!", "错误");
            }


        }

        private void Form2_Load(object sender, EventArgs e)
        {
            
        }

        private void Form2_Shown(object sender, EventArgs e)
        {
            //Respond_date = w_str;
            Judgment();
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //// 取消关闭事件
            //e.Cancel = true;

            //// 隐藏窗体
            //this.WindowState = FormWindowState.Minimized;
        }

        private void Form2_Activated(object sender, EventArgs e)
        {          
            //Judgment();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            this.label3.Text = judgment;

        }

        /// <summary>
        /// 字幕刷新
        /// </summary>
        private void Judgment()
        {
            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    string line;
                    bool flag = false;
                    
                    while ((line = reader.ReadLine()) != null)
                    {
                       // Console.WriteLine(line);
                        string[] delimiters = { " ", " ", " ", " " };
                        string[] result = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        string[] result1 = w_str.Split('.');

                        // 在这里处理每一行数据
                        if (result[0] == "1")//从第一个单词开始
                            flag = true;
                        if(flag)
                        if (int.Parse(result[0]) == int.Parse(result1[0]))
                        {
                            
                            this.label1.Text = result[2];//中文刷新                      
                            judgment = result[1];//英文备份
                        }
                    
                    }
                }
            }
            catch (IOException e1)
            {
                Console.WriteLine("无法读取！");
                Console.WriteLine(e1.Message);
            }
        }
    }
}
